<?php

require_once dirname(__FILE__) . '/Snoopy.class.php';
global $http_status_code;

function snoopy_fetch($url, $boolhost = true, $randip = true, $curr_retry_count = 0, $max_retry_count = 1) {
    global $http_status_code;
    $intTimeStart = microtime(true);
    $logString = 'Fetching:' . $url;

    $ua = array(
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)',
        '(compatible; MSIE 7.01; MSN 2.5; AOL 4.0; Windows XP)',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11',
        'Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)',
        'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        'Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)',
    );
    $uriinfo = parse_url($url);
    $snoopy = new Snoopy;
    $snoopy->agent = $ua[mt_rand(0, count($ua) - 1)];
    if ($boolhost) {
        $snoopy->host = $uriinfo['host'];
    }
    $snoopy->referer = $uriinfo['scheme'] . '://' . $uriinfo['host'] . '/';
    $snoopy->rawheaders["Pragma"] = "no-cache";
    if ($boolhost) {
        $snoopy->rawheaders["Host"] = $uriinfo['host'];
    }
    if ($randip) {
        $snoopy->rawheaders["X_FORWARDED_FOR"] = mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255);
    }
    $snoopy->fetch($url);
    $s = $snoopy->results;
    $http_status_code = $snoopy->status;
    $intTimeEnd = microtime(true);

    log::n('HttpStatusCode:' . $http_status_code . '|RequestTime:' . ($intTimeEnd - $intTimeStart) . 's|' . $logString);

    //加入自动重试功能
    if ($http_status_code == 10060 || '12646132' == $http_status_code || 0 == $http_status_code || substr($http_status_code, 0, 1) == '5') {
        if ($curr_retry_count < $max_retry_count) {
            $curr_retry_count = $curr_retry_count + 1;
            return snoopy_fetch($url, $boolhost, $randip, $curr_retry_count, $max_retry_count);
        } else {
            log::w('Fetch URL[' . $url . '] failed,max_retry_count[' . $max_retry_count . ']');
        }
    }

    return $s;
}

?>